﻿using System;
using System.Data;
using System.Data.Objects;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

namespace ThanNhanTao.Server.BLL
{
    public class CauHinhBLL
    {
        Microsoft.SqlServer.Management.Smo.Server server;
        bvdn_thannhantaoEntities entities;
        public CauHinhBLL()
        {
            entities = ThanNhanTaoGlobalServer.bvdn_thannhantaoEntities;
        }

        public bool CheckExistingDatabase()
        {
            if (!entities.DatabaseExists())
            {
                return false;
            }
            else
	        {
                return true;
	        }
        }

        /// <summary>
        /// Backup db
        /// </summary>
        /// <param name="path"></param>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public bool BackupDB(string path, string dbName, out string fileNameBackup)
        {
            fileNameBackup = string.Empty;
            path = path.Replace(@"\\",@"\");
            ObjectParameter objReturnValue = new ObjectParameter("returnValue", -1);
            ObjectParameter objFileNameBackup = new ObjectParameter("fileNameBackup", fileNameBackup);
            entities.Backup_ThanNhanTao(path, "User", "thannhantao", objReturnValue, objFileNameBackup);
            if (objReturnValue == null || Convert.ToInt32(objReturnValue.Value) < 0)
                return false;
            fileNameBackup = objFileNameBackup.Value.ToString();
            return true;
        }

        public void RestoreDatabase(string databaseName, string filePath,
                                       string serverName, string userName, string password)
        {
            // Create a new connection to the selected server name
            ServerConnection srvConn = new ServerConnection(serverName, userName, password);
            // Log in using SQL authentication instead of Windows authentication
            srvConn.LoginSecure = false;

            // Kill all connection of database is using
            server = new Microsoft.SqlServer.Management.Smo.Server(srvConn);
            server.KillAllProcesses(databaseName);

            // Create a new SQL Server object using the connection we created
            server = new Microsoft.SqlServer.Management.Smo.Server(srvConn);

            // If the user has chosen the file from which he wants the database to be restored
            if (server != null)
            {
                // Create a new database restore operation
                Restore rstDatabase = new Restore();
                // Set the restore type to a database restore
                rstDatabase.Action = RestoreActionType.Database;
                // Overwrite DB when db is existing
                rstDatabase.ReplaceDatabase = true;
                // Set the database that we want to perform the restore on
                rstDatabase.Database = databaseName;
                // Set the backup device from which we want to restore, to a file
                string[] str = new string[1];
                str[0] = @"\";
                string fileName = filePath.Split(str,StringSplitOptions.None)[filePath.Split(str,StringSplitOptions.None).Length - 1];
                BackupDeviceItem bkpDevice = new BackupDeviceItem(filePath, DeviceType.File);
                // Add the backup device to the restore type
                rstDatabase.Devices.Add(bkpDevice);
                // If the database already exists, replace it
                rstDatabase.ReplaceDatabase = true;
                // Perform the restore
                rstDatabase.SqlRestore(server);
            }
        }
    }
}
